/**
* @program: src
*
* @description:
*
* @author: Mr.chen
*
* @create: 2020-11-13 15:56
**/
package checktoken
import (

	"ydd-im/service/api/services/wsservice/account"

	"ydd-im/util/response"

	"errors"
	"github.com/gin-gonic/gin"

)
//type InputCheckSessionIdData struct {
//	Token string `form:"token" json:"token" binding:"required"`
//}
// 判断token的合法性请求r.Use(SetUp())
func SetUp() gin.HandlerFunc {
	return func(c *gin.Context) {
		err := verifyCheckToken(c)
		if err != nil {
			response.ResponseJsonFail(c,response.Response{
				Code:response.ErrorCode,
				Message:err.Error(),
			})
			c.Abort()
			return
		}
		c.Next()
	}
}
func verifyCheckToken(c *gin.Context)  error {
	//var inputcheckSessionIdData InputCheckSessionIdData
	//if err := c.ShouldBind(&inputcheckSessionIdData); err != nil {
	//	return errors.New("参数有误，请重试")
	//}
	token := c.Request.Header.Get("token")
	if len(token) == 0 {
		return errors.New("token不能为空")
	}
	// 验证token，查询token,可以将token存为redis,目前是查询数据库
	// 根据token 查询用户的信息，建立用户指定的数组
	//userInfo,err := account.GetUserInfoByToken(token)
	//if err != nil {
	//	return errors.New(err.Error())
	//}
	//userInfos,ok := userInfo.(models.User)
	//if !ok {
	//	return errors.New("查询用户信息有误")
	//}
	//// 将用户存放为session或者局部变量
	//user, _ := json.Marshal(userInfos)
	user,err := account.GetUserInfoByTokenIsCluster(token)
	if err != nil {
		return errors.New(err.Error())
	}
	c.Set("session_userinfo", string(user))
	return nil
}
